package com.yinhe.music.yhmusic.radio.presenter;

import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.IBaseModel;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.model.RadioListInfo;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;
import com.yinhe.music.yhmusic.radio.contract.IRadioListContract;

/**
 * @author 何晓华
 */
public class RadioListPresenter extends BasePresenter<IBaseModel, IRadioListContract.IRadioListView> implements IRadioListContract.IRadioListPresenter {
    @Override
    public void getRadioList(int pageSize, int page, int bannerNum, int radioType, String sort) {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.getRadioList(pageSize, page, bannerNum, radioType, sort)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(radioListInfo -> {
                    getView().setRadioListUI(radioListInfo);
                }, throwable -> {
                    getView().setLoadMoreFailUI();
                    getView().showMessage(throwable);
                }));
    }

    @Override
    public void getSearchList(int page, int pageSize, String keyword) {
        if (getView() == null) {
            return;
        }

        if (page == 1) {
            getView().showLoading();
        }

        addSubscription(mModel.getSearchList(page, pageSize, keyword, IConstants.RADIO)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(searchInfo -> {
                            getView().hideLoading();
                            getView().setRadioListUI(new RadioListInfo(searchInfo.getPageNum(), searchInfo.getRadioList()));
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }

    @Override
    public void getMyRadioList(int pageSize, int page) {
        if (getView() == null) {
            return;
        }

        if (page == 1) {
            getView().showLoading();
        }

        addSubscription(mModel.getMyRadio(page, pageSize)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(radioListInfo -> {
                            getView().hideLoading();
                            getView().setRadioListUI(radioListInfo);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }

    @Override
    public void getUserRadioList(String userId, int page, int pageSize) {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.getUserContent(userId, IConstants.RADIO, page, pageSize)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(userContent -> {
                            RadioListInfo radioListInfo = new RadioListInfo(userContent.getPageNum(), userContent.getRadioList());
                            getView().setRadioListUI(radioListInfo);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }
}
